package org.meb.lotrlcg.web.services;

import java.util.List;

import javax.inject.Inject;
import javax.persistence.EntityManager;

import org.meb.lotrlcg.db.dao.CardDao;
import org.meb.lotrlcg.db.dao.CardSetDao;
import org.meb.lotrlcg.db.dao.JpaDao;
import org.meb.lotrlcg.db.dao.Query;
import org.meb.lotrlcg.db.model.Card;
import org.meb.lotrlcg.db.model.CardSet;
import org.meb.lotrlcg.db.model.Domain;
import org.meb.lotrlcg.db.query.CardQuery;
import org.meb.lotrlcg.db.util.DatabaseUtils;

public class SearchServiceImpl implements SearchService {

	@Inject
	private EntityManager em;

	@Inject
	private User user;
	
	@Override
	public <T> T findUnique(T example) {
		DatabaseUtils.executePrcSetUserLang(em, user.getLanguage());
		return new JpaDao<T>(em).findUnique(example);
	}

	@Override
	public <T> List<T> find(T example) {
		DatabaseUtils.executePrcSetUserLang(em, user.getLanguage());
		return new JpaDao<T>(em).find(example);
	}
	
	@Override
	public <T> T findUnique(Query<T> query) {
		DatabaseUtils.executePrcSetUserLang(em, user.getLanguage());
		return new JpaDao<T>(em).findUnique(query);
	}

	@Override
	public <T> List<T> find(Query<T> query) {
		DatabaseUtils.executePrcSetUserLang(em, user.getLanguage());
		return new JpaDao<T>(em).find(query);
	}

	@Override
	public Card findUnique(Card example) {
		DatabaseUtils.executePrcSetUserLang(em, user.getLanguage());
		return new CardDao(em).findUnique(example);
	}

	@Override
	public List<Card> find(Card example) {
		DatabaseUtils.executePrcSetUserLang(em, user.getLanguage());
		return new CardDao(em).find(example);
	}

	@Override
	public CardSet findUnique(CardSet example) {
		DatabaseUtils.executePrcSetUserLang(em, user.getLanguage());
		return new CardSetDao(em).findUnique(example);
	}

	@Override
	public List<CardSet> find(CardSet example) {
		DatabaseUtils.executePrcSetUserLang(em, user.getLanguage());
		return new CardSetDao(em).find(example);
	}

	@Override
	public Domain findUnique(Domain example) {
		DatabaseUtils.executePrcSetUserLang(em, user.getLanguage());
		return new JpaDao<Domain>(em).findUnique(example);
	}

	@Override
	public List<Domain> find(Domain example) {
		DatabaseUtils.executePrcSetUserLang(em, user.getLanguage());
		return new JpaDao<Domain>(em).find(example);
	}

	@Override
	public List<Card> findCards(CardQuery query) {
		DatabaseUtils.executePrcSetUserLang(em, user.getLanguage());
		return new CardDao(em).find(query);
	}
}
